import { addShareLog, clickShareLink } from "@/api";
import {
  shareArticleInfo,
  addBuryingPoint,
} from "@/api/modules/career.js";
import { checkShareVipAvailable } from "@/api/modules/vip.js";
export default {
  data() {
    return {
      shareTitle: this.$selfName, // 分享自定义标题
      sharePath: "/pages/index/index", // 分享自定义路径
      imageUrl: "",
      shareQuery: "",
      uniqueCode: "", // 后端返回code
      shareCode: "", // 页面路径参数code
      fromLoginStatus: "",
      shareContent: {
        contentType: "",
        contentTitle: "",
        contentId: "",
      },
      isShareVipAvailable: false,
      shareCareerInfoParams: {},
    };
  },
  onLoad(opt) {
    this.personId = uni.getStorageSync("person-id").slice(2) || undefined;
    this.handleUniqueCode(opt);
    if (opt && opt.fromLoginStatus) {
      if (opt.fromLoginStatus == "company") {
        uni.setStorageSync("fromLoginStatus", "company");
      } else if (opt.fromLoginStatus == "consultant") {
        uni.setStorageSync("fromLoginStatus", "consultant");
      } else {
        uni.setStorageSync("fromLoginStatus", "person");
      }
    }
    //复制链接
    wx.onCopyUrl(() => {
      this.addPersonIdOnPath(3);
      console.log(" wx.onCopyUrl", this.shareQuery);

      return {
        title: this.shareTitle,
        query: this.shareQuery,
      };
    });
    // this.checkShareVipAvailable(opt.positionId);
  },
  //直接分享
  onShareAppMessage(res) {
    this.shareCareerInfo();
    // 职业
    if (this.isCareerIndex || this.isShareArticle) {
      // 来自页面内分享按钮
      if (res.from === "button") {
        // 统计职讯分享
        this.shareArticleInfo({
          articleId: res.target.dataset.articleid,
          shareType: res.target.dataset.sharetype,
        });
        this.shareTitle = res.target.dataset.title;

        this.sharePath =
          "/pagesSub/career/careerInfo/articleDetail?id=" +
          res.target.dataset.articleid;
        this.relatedType &&
          (this.sharePath += "&relatedType=" + this.relatedType);
        this.positionId && (this.sharePath += "&positionId=" + this.positionId);

        this.imageUrl = this.defaultCoverImg || res.target.dataset.thumb;
      } else {
        // 职讯详情页\视频播放页分享统计
        this.shareArticle && this.shareArticle();
      }
    }
    if (this.isBuyPageBuryingPoint) {
      this.addBuryingPointFnShare();
    }
    this.loginStatus = uni.getStorageSync("login-status");
    if (this.sharePath.indexOf("sharePath") < 0) {
      this.sharePath = this.addUrlCode(
        this.sharePath,
        "sharePath",
        this.loginStatus
      );
    }

    //职业大全分享埋点:在分享链接之后加一个sharePersonId,其他分享也可以复用这个id
    //shareChannel:1表示分享给好友
    this.addPersonIdOnPath(1);
    if (this.getShareUrl) {
      // 报告页分享数据埋点
      this.getShareUrl();
      if (res.from === "button") {
        this.isShareReport = true;
      }
    }

    if (this.getShareInfo) {
      // 分享赚钱分享数据埋点
      this.getShareInfo();
    }
    if (
      this.shareContent.contentType &&
      this.sharePath.indexOf("contentType") < 0
    ) {
      this.sharePath += `&contentType=${
        this.shareContent.contentType
      }&contentTitle=${this.shareContent.contentTitle || ""}&contentId=${
        this.shareContent.contentId
      }`;
    }
    if (this.sharePath.indexOf("shareChannel") < 0) {
      this.sharePath += `&shareChannel=1`;
    }
    let defaultObj = {
      title: this.shareTitle,
      path: this.sharePath,
      imageUrl: this.imageUrl,
    };

    if (this.personId) {
      const promise = this.getShareData()
        .then((res) => {
          this.uniqueCode = res.data.uniqueCode;
          let path = this.addUrlCode(
            this.sharePath,
            "uniqueCode",
            this.uniqueCode
          );
          // console.log("发送链接 1", path);
          // console.log("发送链接 2", this.sharePath);
          return {
            title: this.shareTitle,
            path,
            imageUrl: this.imageUrl,
          };
        })
        .catch((err) => {
          console.log("发送链接 3 defaultObj", defaultObj);
          return defaultObj;
        });
      console.log("发送链接 4  sharePath", this.imageUrl);
      return {
        title: this.shareTitle,
        path: this.sharePath,
        imageUrl: this.imageUrl,
        promise,
      };
    }
    // console.log("发送链接 5 defaultObj", defaultObj);
    return defaultObj;
  },
  //朋友圈
  onShareTimeline() {
    // 职讯详情页\视频播放页分享统计
    if (this.isShareArticle) {
      this.shareArticle && this.shareArticle();
    }

    // 报告页分享数据埋点
    if (this.getShareUrl) {
      this.getShareUrl();
    }

    // 分享赚钱分享数据埋点
    if (this.getShareInfo) {
      this.getShareInfo();
    }
    //职业大全分享埋点:在分享链接之后加一个sharePersonId,其他分享也可以复用这个id
    //shareChannel:2表示分享到朋友圈
    this.addPersonIdOnPath(2);

    let img = this.imageUrl;
    // ios分享到朋友圈无法显示重定向图片
    let isIos = uni.getSystemInfoSync().platform == "ios";
    if (
      isIos &&
      this.imageUrl &&
      this.imageUrl.indexOf("upload.yl1001.com/upload/images/view?file=") > -1
    ) {
      img = "";
    }
    if (
      this.shareContent.contentType &&
      this.shareQuery.indexOf("contentType") < 0
    ) {
      this.shareQuery += `&contentType=${
        this.shareContent.contentType
      }&contentTitle=${
        this.shareContent.contentTitle || this.info.title || ""
      }&contentId=${this.shareContent.contentId}`;
    }
    if (this.sharePath.indexOf("shareChannel") < 0) {
      this.sharePath += `&shareChannel=2`;
    }

    if (
      this.sharePath.indexOf("careerInfo/articleDetail") != -1 &&
      this.isShareVipAvailable
    ) {
      this.shareQuery += `&shareType=20`;
    }
    // console.log("发送链接 6 this.shareQuery", this.shareQuery);
    return {
      title: this.shareTitle,
      query:
        "uniqueCode=" +
        this.uniqueCode +
        "&fromLoginStatus=" +
        this.loginStatus +
        "&" +
        this.shareQuery,
      imageUrl: img,
    };
  },
  methods: {
    async addBuryingPointFnShare() {
      const params = {
        etId: 10002, // 传2代表是分享出去
        id: "",
        personId: uni.getStorageSync("person-id").slice(2) || "",
        cardType: uni.getStorageSync("cardType"),
      };
      console.log("params让我看看你是个什么东西", params);
      const res = await addBuryingPoint(params);
      if (res.code == 200) {
        console.log("res11111", res);
      }
    },
    checkShareVipAvailable(positionId) {
      const params = {
        positionId,
      };
      checkShareVipAvailable(params).then((res) => {
        if (res.code == 200) {
          this.isShareVipAvailable = res.data;
        }
      });
    },
    addPersonIdOnPath(shareChannel) {
      const sharePersonId = uni.getStorageSync("person-id").slice(2);

      //分享到朋友要在sharePath加参数
      if (shareChannel == 1) {
        if (this.sharePath.indexOf("?") != -1) {
          //如果是已经有一些自定义参数的时候
          if (this.sharePath.indexOf("&sharePersonId") == -1) {
            this.sharePath += `&sharePersonId=${sharePersonId}&shareChannel=${shareChannel}`;
          }
        } else {
          this.sharePath += `?sharePersonId=${sharePersonId}&shareChannel=${shareChannel}`;
        }

        //分享到朋友圈需要在shareQuery加参数,并且要兼容页面里添加的
      } else if (shareChannel == 2) {
        if (this.shareQuery.indexOf("=") == -1) {
          this.shareQuery = `sharePersonId=${sharePersonId}&shareChannel=${shareChannel}`;
        } else if (this.shareQuery.indexOf("sharePersonId") == -1) {
          this.shareQuery += `&sharePersonId=${sharePersonId}&shareChannel=${shareChannel}`;
        }
      } else if (shareChannel == 3) {
        // 复制链接
        //layerId是维度id,oriPrice是假价格，price是真价格只有在JobDescriptionDetail.vue有用
        if (this.shareQuery && this.shareQuery.indexOf("=") == -1) {
          this.shareQuery = `sharePersonId=${sharePersonId}&layerId=${
            this.layerId
          }&oriPrice=${this.oriPrice * 100}&price=${this.price * 100}`;
        } else {
          this.shareQuery += `&sharePersonId=${sharePersonId}&layerId=${
            this.layerId
          }&oriPrice=${+this.oriPrice * 100}&price=${+this.price * 100}`;
        }
        //在positionDetail页面可能重复
        if (this.shareQuery.indexOf("positionId") == -1) {
          this.shareQuery += `&positionId=${this.positionId}`;
        }
      }

      //如果是职业大全目录下的页面,要额外上报一次
      if (this.sharePath.indexOf("/positionDictionary/") != -1) {
        this.reportPositionDictionaryShare(shareChannel);
      } else if (
        this.sharePath.indexOf("careerInfo/articleDetail") != -1 &&
        shareChannel == 2 &&
        this.isShareVipAvailable
      ) {
        //分享职讯详情的时候有一个版本也用到这个接口
        this.reportPositionDictionaryShare(shareChannel, 20);
      }
    },
    //这个是上报分享动作的 注意结尾是Share
    reportPositionDictionaryShare(shareChannel, shareType) {
      return;
    },
    handleUniqueCode(opt) {
      this.shareCode = opt && opt.uniqueCode;
      if (this.shareCode) {
        getApp().globalData.shareCode = this.shareCode;
        this.reportData();
      }
    },
    getShareData() {
      const userInfo = getApp().globalData.userInfo;
      const params = {
        personId: this.personId,
        pageUrl: this.sharePath,
        pageName: this.shareTitle,
        personUname: userInfo.uname || this.personId,
      };
      return addShareLog(params);
    },
    addUrlCode(url, param, value) {
      if (url.indexOf("?") >= 0) {
        url = url + "&" + param + "=" + value;
      } else {
        url = url + "?" + param + "=" + value;
      }
      return url;
    },
    reportData() {
      if (!this.personId) return;
      const params = {
        personId: this.personId,
        uniqueCode: this.shareCode,
        type: "other",
      };
      clickShareLink(params);
    },
    shareArticleInfo(info) {
      const params = {
        person_id: this.personId,
        condition_arr: JSON.stringify({
          share_type: info.shareType, // share_type 分享类型 1：文章 2：视频
          article_id: info.articleId,
        }),
      };
      shareArticleInfo(params);
    },
    async shareCareerInfo() {
      if (this.shareCareerInfoParams) {
        console.log(
          "shareCareerInfo--------->params让我看看你是个什么东西",
          this.shareCareerInfoParams
        );
        const res = await addBuryingPoint(this.shareCareerInfoParams);
        if (res.code == 200) {
          console.log("res11111", res);
        }
      } else {
        return;
      }
    },
  },
};
